import 'dart:ffi';

import 'package:ffi/ffi.dart';
import 'package:leaf_rust/bindings.dart';
import 'package:leaf_rust/leaf_rust_platform_interface.dart';

class LeafRust {
  static final LeafBindings _bindings = LeafBindings(LeafRust._loadLibrary());

  static DynamicLibrary _loadLibrary() {
    return DynamicLibrary.process();
  }

  static Future<int?> leafRunBg(int rtId, String configPath) {
    return LeafRustPlatform.instance.leafRun(rtId, configPath);
  }

  static int leafRun(int rtId, String configPath) {
    final confPath = configPath.toNativeUtf8().cast<Char>();
    final result = _bindings.leaf_run(rtId, confPath);
    return result;
  }

  static int leafRunWithOptions(
    int rtId,
    String configPath,
    bool autoReload,
    bool multiThread,
    bool autoThreads,
    int threads,
    int stackSize,
  ) {
    final confPath = configPath.toNativeUtf8().cast<Char>();
    final result = _bindings.leaf_run_with_options(rtId, confPath, autoReload,
        multiThread, autoThreads, threads, stackSize);
    return result;
  }

  static int leafRunWithConfigString(int rtId, String configStr) {
    final confStr = configStr.toNativeUtf8().cast<Char>();
    final result = _bindings.leaf_run_with_config_string(rtId, confStr);
    return result;
  }

  static int leafTestConfig(String configPath) {
    final confPath = configPath.toNativeUtf8().cast<Char>();
    final result = _bindings.leaf_test_config(confPath);
    return result;
  }

  static int leafReload(int rtId) {
    final result = _bindings.leaf_reload(rtId);
    return result;
  }

  static bool leafShutdown(int rtId) {
    final result = _bindings.leaf_shutdown(rtId);
    return result;
  }
}
